package org.geekbang.thinking.in.spring.ioc.dependency.source;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.annotation.PostConstruct;

/**
 * ResolvableDependency 作为依赖来源
 *
 * @author wangyh2
 * @version v1.0
 * @since 2021-09-30 16:39
 */
public class ResolvableDependencySourceDemo {

    @Autowired
    private String string;

    @PostConstruct
    public void init() {
        System.out.println(string);
    }

    public static void main(String[] args) {
        // 1, 创建 BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 2, 注册 Configuration Class（配置类） -> Spring Bean
        applicationContext.register(ResolvableDependencySourceDemo.class);
        // 3, 调用 addBeanFactoryPostProcessor 从而注册ResolvableDependency对象
        applicationContext.addBeanFactoryPostProcessor(beanFactory -> {
            beanFactory.registerResolvableDependency(String.class, "Hello World");
        });
        // 4, 启动 Spring 应用上下文
        applicationContext.refresh();
        // 5, 显示地关闭 Spring 应用上下文
        applicationContext.close();
    }
}
